package transformer.courseTransformer;



import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;

import people.Person;
import people.Person.Name;
import permission.role.AdministratorRole;
import transformer.studentTransformer.StudentStringTransformer;
import course.Course;

// change input to lva
public class CourseStringTransformer extends CourseTransformer<String> {
	
	private String coursePrefix, studentPrefix;
	private static final Person ADMIN = new Person(new Name("admin", "admin"));
	static {
		ADMIN.addRole(new AdministratorRole());
	}
	
	public CourseStringTransformer() {
		this(false, null, null);
	}
	
	public CourseStringTransformer(String coursePrefix) {
		this(false, coursePrefix, null);
	}
	
	public CourseStringTransformer(boolean printStudents, String coursePrefix, String studentPrefix) {
		super(printStudents);
		if(coursePrefix == null) {
			this.coursePrefix = "";
		} else {
			this.coursePrefix = coursePrefix;
		}
		
		if(studentPrefix == null) {
			this.studentPrefix = "";
		} else {
			this.studentPrefix = studentPrefix;
		}
	}
	
	@Override
	public String transform(Course course) {
		if(course == null) {
			throw new IllegalArgumentException("Parameter 'course' can not be null!");
		}
		StringBuilder sb = new StringBuilder();
		
		sb.append(getCoursePrefix());
		sb.append(course.getCourseID());
		sb.append(" ");
		sb.append(course.getCourseName());
		
		DateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy");
		
		sb.append("\nregister for course from ");
		sb.append(dateFormatter.format(course.getRegisterStart()));
		sb.append(" till ");
		sb.append(dateFormatter.format(course.getRegisterEnd()));
		sb.append("\nunregister possible till ");
		sb.append(dateFormatter.format(course.getUnregisterEnd()));
		sb.append("\n");
		sb.append(course.getNumberOfRegistrations());
		sb.append(" students registered");
		sb.append("\n");
		
		if(getPrintStudents()) {
			StudentStringTransformer studentTransformer = new StudentStringTransformer(getStudentPrefix());
			studentTransformer.transform(course.getRegisteredStudents(ADMIN));
		}
		
		return sb.toString();
	}

	@Override
	public String transform(Iterable<Course> courses) {
		if(courses == null) {
			throw new IllegalArgumentException();
		}
		
		StringBuilder result = new StringBuilder();
		
		Iterator<Course> it = courses.iterator();
		Course current;
		while(it.hasNext()) {
			current = it.next();
			result.append(this.transform(current));
			if(it.hasNext()) {
				result.append("\n");
			}
		}
		
		return result.toString();
	}

	public String getCoursePrefix() {
		return coursePrefix;
	}

	public void setCoursePrefix(String coursePrefix) {
		this.coursePrefix = coursePrefix;
	}

	public String getStudentPrefix() {
		return studentPrefix;
	}

	public void setStudentPrefix(String studentPrefix) {
		this.studentPrefix = studentPrefix;
	}

}
